package com.tilaiedu.mvc.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tilaiedu.mvc.entity.Goods;
import com.tilaiedu.mvc.mapper.GoodsMapper;
import com.tilaiedu.mvc.service.GoodsService;
import com.tilaiedu.mvc.vo.PageVO;
import com.tilaiedu.mvc.vo.SearchVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @Author xlzhang
 * @Date 2022/5/26 11:32
 * @Version 1.0
 * @Description TODO
 */
@Slf4j
@Service
public class GoodsServiceImpl implements GoodsService {

    @Autowired(required = false)
    private GoodsMapper goodsMapper;

    @Override
    public PageVO<Goods> selectAllWithParam(SearchVO searchVO, int page, int limit) {
        // 设置分页数据
        PageHelper.startPage(page,limit);
        // 查询数据库，根据分页
        List<Goods> goodsList = goodsMapper.selectPageByParam(searchVO);
        log.debug("dataList:{}",goodsList);
        // int a = 10/0;
        // 获取分页的数据对象
        PageInfo<Goods> pageInfo = new PageInfo<>(goodsList);
        // 创建一个分页数据对象
        return new PageVO<Goods>()
                .setDataList(goodsList)
                .setTotal((int) pageInfo.getTotal())
                .setPageNum(pageInfo.getPages())
                .setCurrentPage(page)
                .setFirstPage(pageInfo.isIsFirstPage())
                .setLastPage(pageInfo.isIsLastPage())
                .setSearchVO(searchVO);
    }

    @Override
    public int addGoods(Map<String, Object> goods) {
        return goodsMapper.insertWithMap(goods);
    }

    @Override
    public int deleteGoodsByID(Integer gid) {
        return goodsMapper.deleteByPrimaryKey(gid);
    }

    @Override
    public int updateByGidWithMap(Map<String, Object> goods) {
        return goodsMapper.updateByParam(goods);
    }
}

























